package org.jeecg.modules.wmhb.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.UserConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wmhb.entity.WmContract;
import org.jeecg.modules.wmhb.entity.WmProject;
import org.jeecg.modules.wmhb.entity.WmProjectStatistic;
import org.jeecg.modules.wmhb.entity.WmTask;
import org.jeecg.modules.wmhb.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class ProjectStatisticJob implements Job {
    @Autowired
    private IWmProjectService wmProjectService;
    @Autowired
    private IWmContractService wmContractService;
    @Autowired
    private IWmContractProductService wmContractProductService;
    @Autowired
    private IWmTaskService wmTaskService;
    @Autowired
    private IWmProjectStatisticService wmProjectStatisticService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" =============ProjectStatisticJob============ !  时间:" + DateUtils.getTimestamp()));
        List<WmProject> wmProjectList = wmProjectService.list();
        for(WmProject wmProject: wmProjectList){
            if(!wmProject.getStatus().equals(UserConstant.PROJECT_STATUS_PREPARE)){
                List<WmContract> wmContractList = wmContractService.queryGantContractByWmProjectId(wmProject.getId());
                for(WmContract wmContract: wmContractList){
                    List<String> wmContractIdList = new ArrayList<>();
                    wmContractIdList.add(wmContract.getId());
                    Integer allTaskNum = 0;
                    Integer remainTaskNum = 0;
                    Integer finishTaskNum = 0;

                    //所有合同产品及其下面的所有根任务（生产任务为设计起点任务，非生产任务为根任务）
                    List<String> wmContractProductList = wmContractProductService.selectNodeAndChildrenByContractIdList(wmContractIdList);
                    if(!wmContractProductList.isEmpty()) {
                        List<WmTask> wmTaskList = wmTaskService.selectStatisticTaskByContractProduct(wmContractProductList);
                        for (WmTask wmTask : wmTaskList) {
                            if (wmTask.getStatus().equals(UserConstant.TASK_STATUS_FINISHED)) {
                                finishTaskNum++;
                            }
                            else {
                                remainTaskNum++;
                            }
                            allTaskNum++;
                        }

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String date = format.format(new Date());
                        WmProjectStatistic wmProjectStatistic = wmProjectStatisticService.getDateStatistic(date, wmProject.getId(), wmContract.getId());
                        if (wmProjectStatistic != null) {
                            //修改
                            wmProjectStatistic.setTotalNum(allTaskNum);
                            wmProjectStatistic.setRemainNum(remainTaskNum);
                            wmProjectStatistic.setFinishNum(finishTaskNum);
                            wmProjectStatisticService.updateById(wmProjectStatistic);
                        } else {
                            //新增
                            WmProjectStatistic wmProjectStatisticNew = new WmProjectStatistic();
                            wmProjectStatisticNew.setRemainNum(remainTaskNum);
                            wmProjectStatisticNew.setTotalNum(allTaskNum);
                            wmProjectStatisticNew.setFinishNum(finishTaskNum);
                            wmProjectStatisticNew.setWmProjectId(wmProject.getId());
                            wmProjectStatisticNew.setWmContractId(wmContract.getId());
                            wmProjectStatisticNew.setDate(new Date());
                            wmProjectStatisticService.save(wmProjectStatisticNew);
                        }
                    }
                }
            }
        }
    }
}
